package com.hrd.admin.system.synchlog;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hrd.admin.base.BaseController;
import com.hrd.admin.base.BaseDefine;
import com.hrd.common.util.bean.ServiceResult;
import com.hrd.common.util.paginator.Paginator;
import com.hrd.mybatis.bean.auto.HrdSynchLog;
import com.hrd.mybatis.bean.auto.HrdWarehouseRelation;
import com.hrd.service.SynchLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: wangpeng
 * @Date: 2022/1/17 11:14
 * @Description:
 */
@RequestMapping("/system/synchLog/")
@Controller
public class SynchLogController extends BaseController {
    @Autowired
    private SynchLogService synchLogService;
    /**
     * 列表页面
     *
     * @return
     */
    @RequestMapping("init")
    public ModelAndView init() {
        ModelAndView modelAndView = new ModelAndView("/system/synchlog/synch_log_list");
        return modelAndView;

    }

    @RequestMapping(value = "searchAction")
    public JSONObject searchAction(HttpServletRequest request, @ModelAttribute(BaseDefine.FORM) HrdSynchLog form) {
        // 创建分页
        return this.createPage(request, form);
    }

    /**
     * 创建分页机能
     *
     * @param request
     * @param form
     */
    private JSONObject createPage(HttpServletRequest request, HrdSynchLog form) {

        JSONObject object = new JSONObject();
        // 翻页
        HashMap<String, Integer> pageNumSizeMap = Paginator.getPageNumPageSize(request);
        PageHelper.startPage(pageNumSizeMap.get("pageNum"), pageNumSizeMap.get("pageSize"));
        ServiceResult<List<HrdSynchLog>> result = synchLogService.list(form);

        if (result != null) {
            List<HrdSynchLog> recordList = result.getResult();
            PageInfo paginator = new PageInfo(recordList);// 默认8页数据
            object.put("rows", paginator.getList());
            object.put("total", paginator.getTotal());
        }

        return object;
    }
}
